package com.runen.infinitemovement.widget.popupwind;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.runen.infinitemovement.R;
import com.runen.infinitemovement.model.enums.PayType;

/**
 * Create by LiaoDuanHong
 * Create on
 */
public class PayPopupWindow extends PopupWindow {
    private ImageView iv_dismiss;
    private RelativeLayout rl_wechat, rl_ali;
    private ImageView iv_wechat, iv_ali;
    private TextView tv_pay, tv_desc, tv_update_persons, tv_to_pay;
    private Context context;
    private View layout;
    private int updatePersons;//升级人数
    private int price;//单价
    private int nowPersons;//当前人数
    private int type = 0;//当前选择的支付方式

    public static PayPopupWindow getInstance(Context context, int updatePersons, int price, int nowPersons) {
        return new PayPopupWindow(context, updatePersons, price, nowPersons);
    }

    private PayPopupWindow(Context context, int updatePersons, int price, int nowPersons) {
        super(context);
        this.context = context;
        this.updatePersons = updatePersons;
        this.price = price;
        this.nowPersons = nowPersons;
        initView();
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        //this.setHeight((int) (CommonUtil.getScreenHW((Activity) context)[1] / 3));
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
        // 这样设置才能点击屏幕外dismiss窗口
        this.setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(layout);

        initEvent();
    }

    private void initView() {
        layout = View.inflate(context, R.layout.popup_pay, null);
        iv_dismiss = layout.findViewById(R.id.iv_dismiss);
        rl_wechat = layout.findViewById(R.id.rl_wechat);
        rl_ali = layout.findViewById(R.id.rl_ali);
        iv_wechat = layout.findViewById(R.id.iv_wechat);
        iv_ali = layout.findViewById(R.id.iv_ali);
        tv_pay = layout.findViewById(R.id.tv_pay);
        tv_desc = layout.findViewById(R.id.tv_desc);
        tv_update_persons = layout.findViewById(R.id.tv_update_persons);
        tv_to_pay = layout.findViewById(R.id.tv_to_pay);

        tv_update_persons.setText(updatePersons + "人");
        tv_to_pay.setText(updatePersons * price + "元");
        String text = "立即支付 %s 元";
        tv_pay.setText(String.format(text, updatePersons * price));
        String text1 = "升级后，军团总人数上限为：%s人";
        tv_desc.setText(String.format(text1, nowPersons + updatePersons));

        iv_wechat.setImageResource(R.mipmap.ico_person_normal_check);
        iv_ali.setImageResource(R.mipmap.ico_person_normal_check);
    }


    private void initEvent() {
        iv_dismiss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        rl_wechat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = PayType.WECHAT;
                iv_wechat.setImageResource(R.mipmap.ico_person_checked);
                iv_ali.setImageResource(R.mipmap.ico_person_normal_check);
            }
        });
        rl_ali.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                type = PayType.ALI;
                iv_wechat.setImageResource(R.mipmap.ico_person_normal_check);
                iv_ali.setImageResource(R.mipmap.ico_person_checked);
            }
        });
        tv_pay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2021/4/14  
            }
        });
    }


    public void show() {
//        this.setAnimationStyle(R.style.Animation);
        //防止部分手机底部状态栏与popupWindow重叠
        this.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        this.setAnimationStyle(R.style.AnimationBottomFade);
        //popupWindow底部显示
        this.showAtLocation(((Activity) context).getWindow().getDecorView().findViewById(android.R.id.content), Gravity.BOTTOM, 0, 0);
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();

        lp.alpha = 0.5f;
        if (lp.alpha == 1) {
            ((Activity) context).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//不移除该Flag的话,在有视频的页面上的视频会出现黑屏的bug
        } else {
            ((Activity) context).getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        }
        ((Activity) context).getWindow().setAttributes(lp);
    }

    @Override
    public void dismiss() {
        super.dismiss();
        WindowManager.LayoutParams lp = ((Activity) context).getWindow().getAttributes();
        lp.alpha = 1f;
        ((Activity) context).getWindow().setAttributes(lp);
    }
}
